package ui;

import utils.URLUtil;

import javax.swing.*;
import java.awt.*;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

/**
 * @author Braycep
 * @date 2019/1/7 9:58
 */
public class MainUI extends JFrame {
    private JFrame frame;
    private JTextField url;
    private JButton btn_getPwd;
    private JTextField password;
    private boolean showAction = false;

    public MainUI() throws HeadlessException {
        frame = this;
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setTitle("获取百度网盘分享链接");
        setSize(400, 200);
        setLocationRelativeTo(null);
        setResizable(false);

        initComponents(this);

        setVisible(true);
    }

    private void initComponents(JFrame frame) {
        JPanel panel = new JPanel(null);

        url = new JTextField("请输入链接地址");
        url.setBounds(20, 40, 360, 30);
        url.setFont(new Font("微软雅黑", Font.PLAIN, 15));
        url.setToolTipText("填写分享的链接，链接结尾部分必须以'/1'开始，在默认情况下是正确的");
        panel.add(url);

        btn_getPwd = new JButton("获取");
        btn_getPwd.setFont(new Font("微软雅黑", Font.PLAIN, 15));
        btn_getPwd.setBounds(20, 105, 100, 30);
        btn_getPwd.setToolTipText("点击获取密码");
        panel.add(btn_getPwd);

        password = new JTextField("密码");
        password.setFont(new Font("微软雅黑", Font.PLAIN, 15));
        password.setEditable(false);
        password.setBounds(280, 105, 100, 30);
        password.setHorizontalAlignment(0);
        password.setToolTipText("点击复制密码到剪贴板");
        panel.add(password);

        frame.add(panel);

        initEvents();
    }

    private void initEvents() {
        url.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseReleased(MouseEvent e) {
                String text = url.getText();
                if ("请输入链接地址".equals(text)) {
                    url.setText("");
                }
            }

            @Override
            public void mouseExited(MouseEvent e) {
                String text = url.getText();
                if ("".equals(text)) {
                    url.setText("请输入链接地址");
                }
            }
        });

        url.addKeyListener(new KeyAdapter() {
            @Override
            public void keyReleased(KeyEvent e) {
                if (e.getKeyCode() == KeyEvent.VK_ENTER) {
                    getPassword(url.getText());
                }
            }
        });

        btn_getPwd.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseReleased(MouseEvent e) {
                getPassword(url.getText());
            }
        });

        password.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseReleased(MouseEvent e) {
                String text = password.getText();
                if (!"".equals(text) && !"密码".equals(text)) {
                    Clipboard clip = Toolkit.getDefaultToolkit().getSystemClipboard();
                    Transferable tText = new StringSelection(text);
                    clip.setContents(tText, null);
                    JOptionPane.showMessageDialog(null, "密码已复制", "提示", JOptionPane.INFORMATION_MESSAGE);
                }
            }
        });
    }

    private void getPassword(String url) {
        if ("".equals(url) || "请输入链接地址".equals(url)) {
            JOptionPane.showMessageDialog(this, "请输入链接地址", "警告", JOptionPane.WARNING_MESSAGE);
        } else if (!url.contains("/1") || url.matches(".+\\.\\w+")) {
            JOptionPane.showMessageDialog(this, "链接地址格式不正确", "警告", JOptionPane.WARNING_MESSAGE);
        } else {
            btn_getPwd.setVisible(false);

            final String key = url.substring(url.lastIndexOf("/1") + 2);

            showAction = true;
            showProcess();

            new Thread() {
                @Override
                public void run() {
                    try {
                        String pwd = URLUtil.getPassword(key);
                        showAction = false;
                        password.setText(pwd);
                    } catch (Exception e) {
                        showAction = false;
                        JOptionPane.showMessageDialog(frame, "获取失败", "错误", JOptionPane.ERROR_MESSAGE);
                        password.setText("密码");
                        e.printStackTrace();
                    } finally {
                        btn_getPwd.setVisible(true);
                    }
                }
            }.start();
        }
    }

    private void showProcess() {
        new Thread() {
            @Override
            public void run() {
                while (showAction) {
                    try {
                        password.setText(URLUtil.getRandomWord(4));
                        Thread.sleep(50);
                    } catch (InterruptedException e) {
                        System.out.println("动画线程被中断");
                        e.printStackTrace();
                    }
                }
            }
        }.start();
    }
}
